/*
 * wxhiberlite.h
 *
 *  Created on: 2010-7-15
 *      Author: angelstone
 */

#pragma once

#include "hiberlite.h"
#include "mhstringhashmap.h"

namespace hiberlite {

	//---------------------wxString---------------------------------
	template<class A>
	void hibernate(A& ar, wxString& value, const unsigned int) {
		ar & db_atom<wxString> (value);
	}

	template<> template<class Stmt, class Arg>
	void db_atom<wxString>::loadValue(Stmt& res, Arg& arg) {
		const char * val1 = (const char*) (res.get_text(arg));
		val = wxString::FromUTF8(val1);
	}

	template<>
	inline std::string db_atom<wxString>::sqliteStorageClass() {
		return "TEXT";
	}
	template<>
	inline void db_atom<wxString>::bindValue(sqlite3_stmt* stmt, int col) {
		sqlite3_bind_text(stmt, col, val.ToUTF8().data(), -1, SQLITE_TRANSIENT);
	}


	//---------------------wxDateTime---------------------------------
	template<class A>
	void hibernate(A& ar, wxDateTime& value, const unsigned int) {
		ar & db_atom<wxDateTime> (value);
	}

	template<> template<class Stmt, class Arg>
	void db_atom<wxDateTime>::loadValue(Stmt& res, Arg& arg) {
		val = wxDateTime((((time_t) (res.get_int64(arg)))));
	}

	template<>
	inline std::string db_atom<wxDateTime>::sqliteStorageClass() {
		return "INTEGER";
	}
	template<>
	inline void db_atom<wxDateTime>::bindValue(sqlite3_stmt* stmt, int col) {
		sqlite3_bind_int64(stmt, col, val.GetTicks());
	}


	//---------------------wxArrayString-------------------------------
	template<class A>
	void hibernate(A& ar, wxArrayString& v, const unsigned int) {
		collection_nvp<wxString, stl_stream_adapter<wxString, wxArrayString> >
				body("items", stl_stream_adapter<wxString, wxArrayString> (v));
		ar & body;
	}


	//------------------wxHashmapStr2StrNoCase--------------------------
	template<class A>
	void hibernate(A& ar, mediahouse::wxHashmapStr2StrNoCase::value_type & m,
			const unsigned int) {
		sql_nvp<wxString> f("key", m.first);
		sql_nvp<wxString> s("value", m.second);
		ar & f;
		ar & s;
	}

	template<class A>
	void construct(A& a, mediahouse::wxHashmapStr2StrNoCase::value_type** c,
			const long unsigned int bogus) {
		*c = new mediahouse::wxHashmapStr2StrNoCase::value_type(wxT(""), wxT(""));
	}

	class wxhashmap_adapter {
		typedef mediahouse::wxHashmapStr2StrNoCase::value_type RType;
		typedef mediahouse::wxHashmapStr2StrNoCase::value_type WType;
		typedef mediahouse::wxHashmapStr2StrNoCase C;
		C& ct;
		C::iterator it;

		WType xx;
	public:
		void startLoadFromDb() {
			ct.clear();
			it = ct.begin();
		}

		wxhashmap_adapter(C& _ct) :
			ct(_ct), xx(wxT(""), wxT("")) {
			it = ct.begin();
		}
		void putNext(WType& el) {
			ct.insert(el);
			it = ct.end();
		}
		WType& getNext() {
			xx = *it++;
			return xx;
		}
		bool done() {
			return it == ct.end();
		}
	};

	template<class A>
	void hibernate(A& ar, mediahouse::wxHashmapStr2StrNoCase& m,
			const unsigned int) {
		typedef mediahouse::wxHashmapStr2StrNoCase::value_type ElType;
		collection_nvp<ElType, wxhashmap_adapter> body("items",
				wxhashmap_adapter(m));
		ar & body;
	}

}
